package com.example.mqttclient;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
    private final List<DatabaseHelper.DeviceUsageRecord> records;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

    public HistoryAdapter(List<DatabaseHelper.DeviceUsageRecord> records) {
        this.records = records;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DatabaseHelper.DeviceUsageRecord record = records.get(position);

        // 设置设备名称
        String deviceName = "";
        switch (record.getDevice()) {
            case "light": deviceName = "客厅灯"; break;
            case "curtain": deviceName = "窗帘"; break;
            case "fan": deviceName = "风扇"; break;
            case "ac": deviceName = "空调"; break;
            default: deviceName = record.getDevice();
        }
        holder.deviceName.setText(deviceName);

        // 设置时间范围
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
        String timeRange = dateFormat.format(new Date(record.getStartTime())) + " - " +
                dateFormat.format(new Date(record.getEndTime()));
        holder.timeRange.setText(timeRange);

        // 设置能耗和排放
        holder.energyConsumption.setText(String.format("能耗: %.2f kWh", record.getEnergy()));
        holder.emission.setText(String.format("碳排放: %.2f kgCO₂", record.getEmission()));

        // 根据排放量设置不同颜色
        if (record.getEmission() > 0.5) {
            holder.emission.setTextColor(Color.RED);
        } else {
            holder.emission.setTextColor(Color.BLACK);
        }
    }

    public void updateData(List<DatabaseHelper.DeviceUsageRecord> newRecords) {
        this.records.clear();
        this.records.addAll(newRecords);
        notifyDataSetChanged();
    }

    private String getDeviceDisplayName(String deviceId) {
        switch (deviceId) {
            case "light": return "客厅灯";
            case "curtain": return "窗帘";
            case "fan": return "风扇";
            case "ac": return "空调";
            default: return deviceId;
        }
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView deviceName, timeRange, energyConsumption, emission;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            deviceName = itemView.findViewById(R.id.deviceName);
            timeRange = itemView.findViewById(R.id.timeRange);
            energyConsumption = itemView.findViewById(R.id.energyConsumption);
            emission = itemView.findViewById(R.id.emission);
        }
    }



}